package com.example.hotelsystem.controller.floor;


import code.CodeError;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.hotelsystem.entity.*;
import com.example.hotelsystem.page.Pagesize;
import com.example.hotelsystem.result.Result;
import com.example.hotelsystem.service.FloorService;
import com.example.hotelsystem.service.FloorDetailsService;
import com.example.hotelsystem.service.TDictService;
import com.example.hotelsystem.vo.floor.FloorSaveVO;
import com.example.hotelsystem.vo.floor.FloorViewVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Lee
 * @since 2020-10-14
 */
@RestController
@RequestMapping("/hotelsystem/floor")
@Api(tags = "楼层控制器")
public class FloorController {
    @Autowired
    FloorService floorService;
    @Autowired
    FloorDetailsService FloorDetailsService;
    @Autowired
    TDictService tDictService;

    @ApiOperation("楼层详情页")
    @PostMapping("/view")
    public Result floorView(Long floorId){
        try{
            Floor floor = floorService.getById(floorId);
            FloorViewVO vo = new FloorViewVO();
            BeanUtils.copyProperties(floor,vo);

            QueryWrapper<FloorDetails> query = new QueryWrapper<>();
            query.eq("floor_id",floorId);
            FloorDetails floorDetails = FloorDetailsService.getOne(query);
            BeanUtils.copyProperties(floorDetails,vo);
            vo.setStatusText(tDictService.getDictText(vo.getStatus(),"hotelFloorStatus"));
            return Result.success(vo);
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    @ApiOperation("查看楼层列表")
    @PostMapping("/list")
    public Result floowList(@RequestParam(defaultValue = "1") Integer currentPage){
        Page page = new Page(currentPage, Pagesize.pageSize1);
        IPage<Floor> floorPage=floorService.page(page);
        if(floorPage!=null&&floorPage.getSize()>0){
            for(Floor floor : floorPage.getRecords()){
                Long floorId = floor.getId();
                QueryWrapper<FloorDetails> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("floor_id",floorId);
                FloorDetails fd = FloorDetailsService.getOne(queryWrapper);

                if(fd!=null){
                    floor.setContent(fd.getContent());
                    floor.setImage(fd.getImage());
                }
            }
            return Result.success(floorPage);
        }else {
            return Result.error(CodeError.FLOOR_LIST_NULL,"楼层列表为空");
        }
    }

    @ApiOperation("新增楼层")
    @PostMapping("/save")
    public Result saveFloor(@Valid FloorSaveVO floorSaveVO, BindingResult result){
        if (result.hasErrors()) {
            return Result.error(result.getFieldError().getDefaultMessage());
        }
        Floor floor = new Floor();
        BeanUtils.copyProperties(floorSaveVO,floor);
        QueryWrapper<Floor> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("number",floor.getNumber());
        Floor checkFloor = floorService.getOne(queryWrapper);
        if(checkFloor!=null){
            return Result.error(CodeError.FlOOR_NUMBER_ALREADY,"该楼层号已经存在");
        }
        floorService.save(floor);
        QueryWrapper<Floor> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("number",floor.getNumber());
        Floor newFloor = floorService.getOne(queryWrapper2);
        FloorDetails fd = new FloorDetails();
        fd.setImage(floor.getImage());
        fd.setContent(floor.getContent());
        fd.setFloorId(newFloor.getId());
        FloorDetailsService.saveOrUpdate(fd);
        return Result.success("操作成功");
    }

    @ApiOperation("单独上传楼层照片")
    @PostMapping("/uploadImg")
    public Result uploadImg(MultipartFile file){
        if (!file.isEmpty()) {
            //对文文件的全名进行截取然后在后缀名进行删选。
            int begin = file.getOriginalFilename().indexOf(".");
            int last = file.getOriginalFilename().length();
            //获得文件后缀名
            String a = file.getOriginalFilename().substring(begin, last);
            //判断是否为图片
            if ((".gif").equals(a)||(".GIF").equals(a)||
                    (".jpg").equals(a)||(".JPG").equals(a)||
                    (".PNG").equals(a)||(".png").equals(a)){
                return FloorDetailsService.upLoadImg(file);
            }else {
                return Result.error("请上传png、jpg格式的图片文件");
            }
        }else{
            return Result.error("空文件");
        }

    }

    @ApiOperation("修改楼层信息")
    @PostMapping("/mod")
    public Result modFloor(Floor floor){
        floorService.updateById(floor);
        QueryWrapper<FloorDetails> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("floor_id",floor.getId());
        FloorDetails floorDetails = FloorDetailsService.getOne(queryWrapper);
        floorDetails.setContent(floor.getContent());
        FloorDetailsService.updateById(floorDetails);
        return Result.success("操作成功");
    }

    @ApiOperation("删除楼层")
    @PostMapping("/remove")
    public Result removeFloor(Long id){
        floorService.removeById(id);
        QueryWrapper<FloorDetails> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("floor_id",id);
        FloorDetails floorDetails = FloorDetailsService.getOne(queryWrapper);
        FloorDetailsService.removeById(floorDetails.getId());
        return Result.success("操作成功");
    }
}

